//
//  MemberRoamingSVC.m
//  SelfDriving
//
//  Created by apple on 16/1/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MemberRoamingSVC.h"
#import "MemberRoamingCell.h"
#import "BusinessService+FJ.h"
#import "FJModel.h"
#import "FriendDetailsMainVC.h"
//#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
@interface MemberRoamingSVC ()<UICollectionViewDataSource,UICollectionViewDelegate,BaseCollectionDelegate,MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet BaseCollection *collectionView;
@property (weak, nonatomic) IBOutlet MAMapView *mapView;

@end

@implementation MemberRoamingSVC{

    
    NSArray *addata;
    UILabel *notlabel;
    NSMutableArray *data;
    NSMutableArray *datas;
    int isdw;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray alloc]init];
    datas = [[NSMutableArray alloc]init];
    
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    MAPointAnnotation  *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = _coor;
    CLLocation *c = [[CLLocation alloc] initWithLatitude:_coor.latitude longitude:_coor.longitude];
    CLGeocoder *revGeo = [[CLGeocoder alloc] init];
    [revGeo reverseGeocodeLocation:c
                 completionHandler:^(NSArray *placemarks, NSError *error) {
                     if (!error && [placemarks count] > 0)
                     {
                         NSDictionary  *dict = [[placemarks objectAtIndex:0] addressDictionary];
                         pointAnnotation.title = [dict objectForKey:@"SubLocality"];
                         pointAnnotation.subtitle = [dict objectForKey:@"Name"];
                         [_mapView addAnnotation:pointAnnotation];
                     }}];
 
    self.automaticallyAdjustsScrollViewInsets = NO;//处理UICollectionView预留空白
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.bdelegate = self;
 
}

#pragma  mark 高德
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation)
    {
        if (isdw == 0) {
            _mapView.centerCoordinate = _coor;
            isdw = 1;
        }
    }
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    //  if ([annotation isKindOfClass:[MAAnnotationView class]]) {
    if (mapView.userLocation == annotation) {
        return nil;
    }
    static NSString *identify = @"annotation";
    //在原有的大头针中添加自定义的修饰
    MAPinAnnotationView *pointAnnotation = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
    if (pointAnnotation == nil) {
        //在原有的大头针中创建一个新的自定义的大头针
        pointAnnotation = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identify];
    }
    //设置是否能选中的标题
    pointAnnotation.canShowCallout = YES;
    //是否允许拖拽
    pointAnnotation.draggable = YES;
    //是否允许退拽动画
    pointAnnotation.animatesDrop = YES;
    return pointAnnotation;
}
-(void)initData:(int)index{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%f",_coor.latitude] forKey:@"lat"];
    [dic setObject:[NSString stringWithFormat:@"%f",_coor.longitude] forKey:@"lng"];
    [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
    [[BusinessService shareInstance]nearby:dic success:^(id result) {
        //         datas = [_collection result:result olddata:datas index:index];
        datas = result;
        LoginResponseModel *model = [datas firstObject];
        if (index == 1) {
            [data removeAllObjects];
        }
        [data addObjectsFromArray:model.personlist];
        if (notlabel) {
            [notlabel removeFromSuperview];
        }
        notlabel = (UILabel *)[_collectionView notData];
        if (data.count == 0) {
            [self.view addSubview:notlabel];
        }
        [_collectionView reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];



}
-(void)viewWillAppear:(BOOL)animated
{
//    [_mapView viewWillAppear];
//    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
//    [_mapView viewWillDisappear];
//    _mapView.delegate = nil; // 不用时，置nil
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return data.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MemberRoamingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberRoamingCell" forIndexPath:indexPath];
    NSMutableDictionary *dic = [data objectAtIndex:indexPath.row];
    LoginResponseModel *model = [[LoginResponseModel alloc]init];
    [model serializationWithDictionary:dic];
    
    cell.name_label.text = model.nickname;
//    [ValidateHelper setImage:model.headsmall img:cell.head_img istoux:YES];
    [ValidateHelper setImage:model.headsmall img:cell.head_img istoux:NO];
    
    NSString *age = [NSString stringWithFormat:@"%@岁",[ValidateHelper timeToAge:model.age]];
    NSString *job = model.job;
    NSString *distance = [ValidateHelper DistanceFormat:model.distance];
    cell.sign_lable.text = [NSString stringWithFormat:@"%@ %@ %@",age,job,distance];
    if([model.memberlevel intValue]!=0){
        cell.vip_img.hidden = NO;
    }else{
        cell.vip_img.hidden = YES;
    }
    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCRE_WIDTH-30)/2, (SCRE_WIDTH-30)/2);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dic = [data objectAtIndex:indexPath.row];
    LoginResponseModel *model = [[LoginResponseModel alloc]init];
    [model serializationWithDictionary:dic];
    
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = model.uid;
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = mode;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
